﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]  
    public class SupportPolicyController : AdminBaseController
    {
        #region install

        public decimal Year{
            get{
                return Convert.ToDecimal(OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR));
            }
        }

        private string _strYearWhere;
        private BLL.SupportPolicy _spBll = new BLL.SupportPolicy();       

        public SupportPolicyController()
        {
            _strYearWhere = "";
        }

        
        /// <summary>
        /// 当前年度企业性质
        /// </summary>
        public void InstallEnt()
        {
            var enterpriseProps = new BLL.EnterpriseProp().GetModelList(_strYearWhere);
            ViewBag.EnterpriseProps = enterpriseProps;
        }


        /// <summary>
        /// 当前年度可补贴税种
        /// </summary>
        public void InstallTaxClassAllowance()
        {
            var taxClassAllowance = new BLL.TaxClassAllowance().GetModelList("FYear= "+OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR));
            ViewBag.TaxClassAllowance = taxClassAllowance;
        }

        #endregion

        [AuthorityActionFilter("m_SupportPolicy", Order = 1)]
        [LogPageActionFilter("扶持政策定义", Order = 2)]
        public ActionResult Index()
        {
            InstallEnt();
            InstallTaxClassAllowance();
            var policysInfor = _spBll.GetInfor(Year);
            return View(policysInfor);
        }

        public ActionResult SuppoetPolicyPartial()
        {

            string NSRMC = Request["NSRMC"];

            string FEnterprisePropNo = Request["FEnterprisePropNo"];
            string FProp = Request["FProp"];
            string FTaxClassAllowanceNo = Request["FTaxClassAllowanceNo"];
            string strWhere = "1=1";
            if (!string.IsNullOrEmpty(NSRMC))
            {
                strWhere += " and  FPolicyName like '%" + NSRMC + "%'";
            }
            if (!string.IsNullOrEmpty(FProp))
            {

                strWhere += "  and FProp='" + FProp + "'";
            }
            if (!string.IsNullOrEmpty(FEnterprisePropNo))
            {
                strWhere += " and FEnterprisePropNo= '" + FEnterprisePropNo + "'";
            }
            if (!string.IsNullOrEmpty(FTaxClassAllowanceNo))
            {
                strWhere += "  and FTaxClassAllowanceNo='" + FTaxClassAllowanceNo + "'";
            }
             DataTable policysInfor =new DataTable();
            DataRow[]  dtr = _spBll.GetInfor(Year).Select(strWhere);
            if (dtr.Count()>0)
            {
                 policysInfor =dtr.CopyToDataTable();
            }
         
            return PartialView(policysInfor);
        }

        // 保存
        public bool Save(Model.SupportPolicy p)
        {
            var isSucceed = false;

            if (p.IsEvalBase)
            {
                //p.FRates = null;
            }
            else
            {
                p.FInnerRate = 0;
                //p.FOuterRate = null;
            }

            p.FUpdDate = DateTime.Now;
            p.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            p.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
            p.FUpdIP = GenerateUtil.GetClientIPv4Address();

            if(string.IsNullOrEmpty(p.FPolicyID))
            {
                p.FInsDate = p.FUpdDate;
                p.FInsEmpNo = p.FUpdEmplNo;
                p.FInsDeptNo = p.FUpdDeptNo;
                p.FInsIP = p.FUpdIP;
                isSucceed = _spBll.Add(p);
            }
            else
            {
                isSucceed = _spBll.Update(p);
            }

            return isSucceed;
        }

        // 删除
        public bool Delete(string id)
        {
            return _spBll.Delete(id, Year);
        }

        //扶持政策录入编辑
        public ActionResult PolicyEdit(string id)
        {
            InstallEnt();
            InstallTaxClassAllowance();
            var policy = string.IsNullOrEmpty(id) ? new Model.SupportPolicy(Year) : _spBll.GetModel(id, Year);
            return View(policy);
        }

        //条件公式编辑
        public ActionResult RuleFormula(string id)
        {
            InstallTaxClassAllowance();
            return View();
        }

        //计算公式编辑
        public ActionResult ComputeFormula(string id)
        {
            return View();
        }

        public string CheckRuleExpression(string expression)
        {
            try
            {
                DataTable dt = new DataTable();
                BLL.TaxClassAllowance taxClassAllowanceBLL = new BLL.TaxClassAllowance();
                List<Model.TaxClassAllowance> tCAL = taxClassAllowanceBLL.GetModelList("FYear=" + OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR));

                foreach (Model.TaxClassAllowance tCA in tCAL)
                {
                    dt.Columns.Add(tCA.FTaxClassAllowanceNo, typeof(decimal));
                }
                DataRow dr = dt.NewRow();
                foreach (Model.TaxClassAllowance tCA in tCAL)
                {
                    dr[tCA.FTaxClassAllowanceNo] = 10000;
                }
                dt.Rows.Add(dr);
                DataRow[] drarr = dt.Select(expression);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        public string checkExpression(string expression)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("se",typeof(decimal));
                dt.Columns.Add("base", typeof(decimal));
                dt.Columns.Add("rateIn", typeof(decimal));
                dt.Columns.Add("rateOut", typeof(decimal));
                dt.Columns.Add("zcjd");
                DataRow dr = dt.NewRow();
                dr["se"] = 20000;
                dr["base"] = 10000;
                dr["rateIn"] = 0.1;
                dr["rateOut"] = 0.2;
                dt.Rows.Add(dr);
                dt.Columns["zcjd"].Expression = expression;
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

    }
}
